﻿Imports System.IO
Imports System.Xml.Serialization
Public Enum MailServerType
    POP3
    IMAP
End Enum

<XmlRoot("Account")> _
Public Class AccountSettings

    Private _Username As String
    Private _Password As String
    Private _ServerIp As String
    Private _ServerPort As String
    Private _AccountName As String
    Private _Plugin As String
    Private _IsSSLEnabled As Boolean = False
    Private _ServerType As String
    Private _IsEnabled As Boolean = True
    <XmlElement("IsEnabled")> _
        Public Property IsEnabled() As Boolean
        Get
            Return Me._IsEnabled
        End Get
        Set(ByVal value As Boolean)
            Me._IsEnabled = value
        End Set
    End Property
    <XmlElement("ServerType")> _
    Public Property ServerType() As String
        Get
            Return _ServerType
        End Get
        Set(ByVal value As String)
            _ServerType = value
        End Set
    End Property
    <XmlElement("SSLEnabled")> _
    Public Property IsSSLEnabled() As Boolean
        Get
            Return Me._IsSSLEnabled
        End Get
        Set(ByVal value As Boolean)
            Me._IsSSLEnabled = value
        End Set
    End Property
    <XmlElement("Username")> _
    Public Property Username() As String
        Get
            Return Me._Username
        End Get
        Set(ByVal value As String)
            Me._Username = value
        End Set
    End Property
    <XmlElement("Password")> _
    Public Property Password() As String
        Get
            Return Utility.Decrypt(Me._Password)
        End Get
        Set(ByVal value As String)
            Me._Password = Utility.Encrypt(value)
        End Set
    End Property
    <XmlElement("ServerIp")> _
    Public Property ServerIp() As String
        Get
            Return IIf(Me._ServerIp IsNot Nothing, Me._ServerIp, String.Empty)
        End Get
        Set(ByVal value As String)
            Me._ServerIp = value
        End Set
    End Property
    <XmlElement("Port")> _
    Public Property ServerPort() As String
        Get
            Return Me._ServerPort
        End Get
        Set(ByVal value As String)
            Me._ServerPort = value
        End Set
    End Property
    <XmlElement("AccountName")> _
    Public Property AccountName() As String
        Get
            Return Me._AccountName
        End Get
        Set(ByVal value As String)
            Me._AccountName = value
        End Set
    End Property
    <XmlElement("Plugin")> _
    Public Property Plugin() As String
        Get
            Return Me._Plugin
        End Get
        Set(ByVal value As String)
            Me._Plugin = value
        End Set
    End Property

    Public Sub SaveConfig(ByVal filename As String)
        Dim serializer As XmlSerializer
        Using sw As New StreamWriter(filename)
            serializer = New XmlSerializer(GetType(AccountSettings))
            serializer.Serialize(sw, Me)
        End Using
    End Sub
    Public Function ReadConfig(ByVal filename As String) As AccountSettings
        Dim result As AccountSettings
        Dim serializer As XmlSerializer
        Using sr As New StreamReader(filename)
            serializer = New XmlSerializer(GetType(AccountSettings))
            result = TryCast(serializer.Deserialize(sr), AccountSettings)
        End Using
        Return result
    End Function
End Class